package com.shouer.doc.ui.splash;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.single.EmptyPermissionListener;
import com.shouer.doc.R;
import com.shouer.doc.application.App;
import com.shouer.doc.common.util.AppUtil;
import com.shouer.doc.common.util.SPUtils;
import com.shouer.doc.dialog.CommonDialogUtil;
import com.shouer.doc.ui.login.LoginActivity;
import com.shouer.doc.ui.main.MainActivity;
import com.shouer.doc.ui.splash.presenter.ISplashPresenter;
import com.shouer.doc.ui.splash.presenter.SplashPresenterCompl;
import com.shouer.doc.ui.splash.response.VersionUpdateResponse;
import com.shouer.doc.ui.splash.view.ISplashView;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.shouer.doc.common.util.SPUtils.get;

/**
 * Created by ZXL on 2016/12/15.
 */

public class SplashActivity extends AppCompatActivity implements ISplashView, View.OnClickListener {

    private App app;
    private ISplashPresenter presenter;
    private VersionUpdateResponse versionInfo;
    private String userPhone, password;

    @BindView(R.id.version_info)
    protected TextView versionInfoView;

    private String versionString;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_splash_layout);
        ButterKnife.bind(this);
        versionString = AppUtil.getVersion(this);
        versionInfoView.setText("首儿医生的版本号" + versionString);
        app = (App) getApplicationContext();
        presenter = new SplashPresenterCompl(this);
        userPhone = (String) get(this, "user_phone", "");
        password = (String) get(this, "user_pass", "");
        presenter.checkVersion(AppUtil.getVersion(this));

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public App getApp() {
        return app;
    }

    @Override
    public void checkVersionSuccess(VersionUpdateResponse versionInfo) {

        this.versionInfo = versionInfo;
        switch (versionInfo.getUpdateTag()) {
            case "0"://已经是最新版本，不需要升级
                autoLogin();
                break;
            case "1"://可升级
                CommonDialogUtil.getInstance().showMessage(this, this, versionInfo.getReason(), "取消", "更新", 1);
                break;
            case "2"://强制升级
                CommonDialogUtil.getInstance().showMessage(this, this, versionInfo.getReason(), "取消", "更新", 2);
                break;
        }

    }


    @Override
    public void checkVersionFailed() {
        autoLogin();
    }

    @Override
    public void autoLoginSuccess() {
        if (null!=getApp().getManService())
        {
            getApp().getManService().getMANAnalytics().updateUserAccount(getApp().getUserModel().getUserPhone(), getApp().getUserDocId());
        }
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();

    }

    @Override
    public void autoLoginFailed() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private void autoLogin() {
        if (TextUtils.isEmpty(userPhone) || TextUtils.isEmpty(password)) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            Integer isReadXy = (Integer) SPUtils.get(SplashActivity.this, "xieyi", 0);
            if (isReadXy == 1) {
                presenter.autoLogin(userPhone, password);
            } else {
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dialog_left_btn:
                CommonDialogUtil.getInstance().dismiss();
                int tag = (int) view.getTag(R.id.dialog_btn_tag);
                if (tag == 1) {
                    autoLogin();
                } else if (tag == 2) {
                    finish();
                }
                break;
            case R.id.dialog_right_btn:
                CommonDialogUtil.getInstance().dismiss();
                if (Dexter.isRequestOngoing()) {
                    return;
                }
                Dexter.checkPermission(downloadPermissionListener, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                break;
        }
    }


    EmptyPermissionListener downloadPermissionListener = new EmptyPermissionListener() {

        @Override
        public void onPermissionGranted(PermissionGrantedResponse response) {

            presenter.uploadApplication(versionInfo);

        }

        @Override
        public void onPermissionDenied(PermissionDeniedResponse response) {

            Toast.makeText(SplashActivity.this, "没有存储空间权限,请打开权限后下载最新版本安装包", Toast.LENGTH_SHORT).show();
        }

    };
}
